---@class Battle.SkillBufferComponent:Battle.SkillComponent
local SkillBufferComponent = Battle.CreateClass("SkillBufferComponent",Battle.SkillComponent)
Battle.SkillBufferComponent = SkillBufferComponent

function SkillBufferComponent:SetData(isSelf, type, bufferId, layer)
    self.type = type
    self.bufferId = bufferId
    self.layer = layer
    self.isSelf = isSelf
    self.isHitCallback = true
    self.cbType = Battle.Event.ActorDamageAfter
    if isSelf then
        if type == 1 then
            self.isHitCallback = false
            self.cbType = Battle.Event.SkillFrontSwingFinished
        elseif type == 2 then
            self.isHitCallback = false
            self.cbType = Battle.Event.SkillEnd
        else -- 技能开始
            self.isHitCallback = false
            self.cbType = Battle.Event.SkillStart
        end
    end
    if self.isHitCallback then
        self.skill.actor.eventSystem:Register(self.cbType,self.HitCallback,self)
    else
        self.skill.actor.eventSystem:Register(self.cbType,self.SkillCallback,self)
    end

end
function SkillBufferComponent:OnDestroy()
    if self.isHitCallback then
        self.skill.actor.eventSystem:UnRegister(self.cbType,self.HitCallback,self)
    else
        self.skill.actor.eventSystem:UnRegister(self.cbType,self.SkillCallback,self)
    end
end
---@param args BattleEvent
function SkillBufferComponent:SkillCallback(args)
    if self.skill ~= args.skill then return end
    if self.isSelf then
        self.skill.actor:AddBuff(self.bufferId,self.layer,nil,self.skill.actor,self.skill.skillId)
    else
        logError("buff 回调未实现")
    end
end
---@param args Battle.HitArgs
function SkillBufferComponent:HitCallback(args)
    local data = args.damageData
    if data.sourceType == Battle.SourceType.Skill then
        if data.attacker ~= self.skill.actor then
            return
        end
    end
    if data.sourceId ~= self.skill.skillId then
        return
    end
    if self.isSelf then
        self.skill.actor:AddBuff(self.bufferId,self.layer,nil,self.skill.actor,self.skill.skillId)
    else
        local flag = true
        local hurtType = data.hurtType
        local damage = data.damage
        local target = data.target
        if self.type == 1 and (hurtType > 0 and damage == 0) then
            flag = false
        end
        if flag then
            if target and not target:IsDead() then
                target:AddBuff(self.bufferId,self.layer,nil,self.skill.actor,self.skill.skillId)
            end
        end
    end


end